package com.techsoul.module.basic.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.BusinessException;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.NumberUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.common.security.util.SecurityUtils;
import com.techsoul.module.basic.mapper.*;
import com.techsoul.module.basic.pojo.entity.*;
import com.techsoul.module.basic.pojo.param.CustomerSaveParam;
import com.techsoul.module.basic.pojo.param.CustomerSearchParam;
import com.techsoul.module.basic.pojo.param.SupplierSearchParam;
import com.techsoul.module.basic.pojo.vo.BaoliCompanyVo;
import com.techsoul.module.basic.pojo.vo.CustomerVo;
import com.techsoul.module.basic.pojo.vo.StoreVo;
import com.techsoul.module.basic.pojo.vo.SupplierVo;
import com.techsoul.module.basic.service.ICustomerService;
import com.techsoul.module.goods.mapper.SkuMapper;
import com.techsoul.module.goods.mapper.SpuMapper;
import com.techsoul.module.goods.pojo.entity.Sku;
import com.techsoul.module.goods.pojo.entity.Spu;
import com.techsoul.module.pms.mapper.TWhWarehouseCustomerMaterialMapper;
import com.techsoul.module.pms.pojo.entity.TWhWarehouseCustomerMaterial;
import com.techsoul.module.pms.pojo.vo.TWhWarehouseCustomerMaterialVo;
import com.techsoul.module.system.mapper.UserMapper;
import com.techsoul.module.system.mapper.UserRoleRelMapper;
import com.techsoul.module.system.pojo.entity.User;
import com.techsoul.module.system.pojo.entity.UserRoleRel;
import com.techsoul.module.system.pojo.param.UserPostRelSaveParam;
import com.techsoul.module.system.pojo.param.UserRoleRelSaveParam;
import com.techsoul.module.system.pojo.param.UserSaveParam;
import com.techsoul.module.system.pojo.param.UserSearchParam;
import com.techsoul.module.system.service.IUserRoleRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 客户 Service实现
 *
 * @author liuen
 * @date 2021-01-26
 */
@Service
@Transactional(readOnly = true)
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleRelMapper userRoleRelMapper;

    @Autowired
    private CustomerMapper mapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;


    @Autowired
    private TbWmsContainBoxTypeMapper tbWmsContainBoxTypeMapper;

    @Autowired
    private MyCalendarMapper myCalendarMapper;

    @Autowired
    private TWhWarehouseCustomerMaterialMapper tWhWarehouseCustomerMaterialMapper;
    @Autowired
    private BaoliCompanyMapper baoliCompanyMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private IUserRoleRelService userRoleRelService;
    @Autowired
    private TokenService tokenService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(CustomerSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        Customer entity = new Customer();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }
    /**
     * 申请处理批准
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String prove(CustomerSaveParam params) {
        List<String> ids = params.getIds();
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        for(int i=0; i<ids.size(); i++){
            String id= ids.get(i);
            Customer customer
                    =this.mapper.selectById(id);
            if(params.getProveStatus().equals("prove")){
                customer.setProveStatus("prove");
            }else{
                customer.setProveStatus("reject");
            }
            if(params.getRemark()!=null&&params.getRemark()!=""){
                String proveRemark = " 复核人:".concat(sysUser.getUserName()).concat(" 审核意见:").concat(params.getRemark());
                if(!StringUtils.isNull(customer.getRemark())){

                    proveRemark = customer.getRemark().concat(proveRemark);
                }
                customer.setRemark(proveRemark);
            }

            customer.preUpdate(sysUser.getId());
            this.mapper.updateById(customer);
            if(customer.getCustomerType()!=null&&customer.getCustomerType()!=""){
                UserSearchParam userSearchParam = new UserSearchParam();
                if(customer.getCustomerType().equals("consumer_customer")){
                    userSearchParam.setStoreId(customer.getId());
                }else if(customer.getCustomerType().equals("supplier_customer")){
                    userSearchParam.setSupplierId(customer.getId());
                }else if(customer.getCustomerType().equals("consignee_customer")){
                    userSearchParam.setConsigneeId(customer.getId());
                }else{
                    userSearchParam.setCustomerId(customer.getId());
                }

                List<User> users= this.userMapper.findEntityAll(userSearchParam);
                for(int j=0;j<users.size();j++){
                    User user = (User)users.get(j);
                    user.setIsValid("1");
                    user.setCompanyId(customer.getId());
                    user.preUpdate(sysUser.getId());

                    this.userMapper.updateById(user);
                }
            }

            this.addCalendar(customer,sysUser);
        }
        return "success";
    }

    public void addCalendar(Customer object,UserVo sysUser){
        List<MyCalendar> existMycalendar=
                myCalendarMapper.findCalendarByShipperAll(object.getId());
        if(existMycalendar==null||existMycalendar.size()==0){
            List calendars =
                    myCalendarMapper.findAllCalendarByShipper("1000");
            for(int i=0;i<calendars.size();i++){

                MyCalendar newMycalendar = new MyCalendar();
                MyCalendar templateCalendar =(MyCalendar)calendars.get(i);
                try {
                    BeanUtils.copyProperties(newMycalendar, templateCalendar);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                newMycalendar.setShipperCode(object.getId());
                newMycalendar.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                this.myCalendarMapper.insert(newMycalendar);

            }


        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }
    /**
     * 批量作废
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer logicalDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            for(int i=0;i<idList.size();i++){
                String id = (String)idList.get(i);
                Customer entityFromDb = this.mapper.selectById(id);

                UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

                entityFromDb.preUpdate(user.getId());
                entityFromDb.setIsValid("0");
                this.mapper.updateById(entityFromDb);
            }
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(CustomerSaveParam params) {
        Customer entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public CustomerVo findById(String id) {
        CustomerVo customerVo = this.mapper.findById(id);
        return customerVo;
    }
    /**
     * 详情
     *
     * @return
     */
    @Override
    public CustomerVo getMyCustInfor() {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String id = user.getCustomerId();
        String suppid = user.getSupplierId();
        String baoliId = user.getBaoliCompanyId();
        if(id!=null&&id!=""){
            CustomerVo customerVo = this.mapper.findById(id);
            customerVo.setCustType("customer");
            return customerVo;
        }else if(suppid!=null&&suppid!="") {
            id = user.getStoreId();
            SupplierVo supplierVo = this.supplierMapper.findById(suppid);
            if (supplierVo != null) {
                Customer customer = new Customer();
                CustomerVo customerVo = new CustomerVo();
                customer.setCustomerType("supplier");
                customer.setCustomerName(supplierVo.getSupplierName());

                customer.setContactPhone(supplierVo.getContactPhone());
                customer.setContactName(supplierVo.getContactName());
                customer.setCompanyName(supplierVo.getSupplierName());
                customer.setCustomerName(supplierVo.getSupplierName());
                BeanUtils.copyProperties(customer,customerVo);

                customer.setId(idWorker.nextId() + "")
                        .preInsert("1");
                customerVo.setId(customer.getId());
                this.mapper.insert(customer);
                return customerVo;
            }
        }else if(baoliId!=null&&baoliId!="") {

            BaoliCompanyVo baoliCompanyVo = this.baoliCompanyMapper.findById(baoliId);
            if (baoliCompanyVo != null) {
                Customer customer = new Customer();
                CustomerVo customerVo = new CustomerVo();
                customer.setCustomerType("baoli");
                customer.setCustomerName(baoliCompanyVo.getCompanyName());

                customer.setContactPhone(user.getPhone());
                customer.setContactName(user.getNickName());
                customer.setCompanyName(baoliCompanyVo.getCompanyName());

                BeanUtils.copyProperties(customer,customerVo);
                customer.setId(idWorker.nextId() + "")
                        .preInsert("1");
                customerVo.setId(customer.getId());
                this.mapper.insert(customer);
                return customerVo;
            }
        }else {
            id = user.getStoreId();
            StoreVo storeVo = this.storeMapper.findById(id);
            if(storeVo!=null){
                CustomerVo customerVo = new CustomerVo();
                Customer customer = new Customer();

                customer.setCustomerType("buyer");
                customer.setCredit(storeVo.getCredit());
                customer.setCreditLeft(storeVo.getCreditLeft());
                customer.setFrezeCredit(storeVo.getFrezeCredit());
                customer.setCustomerName(storeVo.getStoreName());
                customer.setBaoliCompanyName(storeVo.getBaoliCompanyName());
                customer.setContactPhone(user.getPhone());
                customer.setContactName(user.getNickName());

                customer.setCompanyName(storeVo.getStoreName());

                BeanUtils.copyProperties(customer,customerVo);
                customer.setId(idWorker.nextId() + "")
                        .preInsert("1");
                this.mapper.insert(customer);
                customerVo.setId(customer.getId());
                return customerVo;
            }

        }

        return null;
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<CustomerVo> findAll(CustomerSearchParam params) {
        List<CustomerVo> customerVos = this.mapper.findAll(params);
        return customerVos;
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<CustomerVo> findAll(CustomerSearchParam params, Integer current, Integer pageSize) {
        IPage<CustomerVo> voIPage = this.mapper.findAll(new Page<>(current, pageSize), params);
        return voIPage;
    }
    /**
     * 通过编码查询
     * @param code
     * @return
     */
    @Override
    public CustomerVo findByCode(String code) {
        return this.mapper.findByCode(code);
    }
    /**
     * 校验客户编码
     * @param params
     * @return
     */
    @Override
    public Boolean checkCode(CustomerSearchParam params) {
        CustomerVo storeVo = this.findByCode(params.getCustomerCode());
        if (storeVo == null) {
            return true;
        } else {
            if (storeVo.getId().equals(params.getId())) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Transactional(readOnly = false)
    @Override
    public void fixdata() {
        List<Customer> customers = this.mapper.selectList(new QueryWrapper<>());
        for (Customer customer : customers) {
            this.mapper.updateById(customer);
        }
    }

    @Transactional(readOnly = false)
    @Override
    public String insertBizUser(UserSaveParam params) {
        //  UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        // 电话号码加密
        // 新增用户
        User entity = new User();
        UserVo oldentity = this.userMapper.findByUserName(params.getPhone());
        if(oldentity!=null){
            throw new BusinessException("手机号码已经存在，请换一个手机号码注册");
        }
        BeanUtils.copyProperties(params, entity);
        entity.setPassword(SecurityUtils.encryptPassword(params.getPassword()));
        entity.setUserName(params.getPhone());


        if(entity.getNickName()==null||entity.getNickName()==""){
            entity.setNickName(params.getUserName());
        }
        if(params.getUserType().equals("企业")){
            if(params.getCustomerId()!=null&&params.getCustomerId()!=""){
                entity.setOrgId("1512956779477209088");
                entity.setSite(params.getCustomerId());

            }else{
                Customer customer = new Customer();
                customer.setContactPhone(params.getPhone());
                customer.setContactName(params.getUserName());
                customer.setCompanyName(params.getCompanyName());
                customer.setCustomerName(params.getCompanyName());
                customer.setProveStatus("NEW");
                customer.setCustomerType("企业");
                customer.setId(idWorker.nextId() + "")
                        .preInsert("1");
                this.mapper.insert(customer);
                entity.setOrgId("1512956779477209088");
                entity.setSite(params.getCustomerId());
                entity.setCustomerId(customer.getId());
            }
        }else if(params.getUserType().equals("银行")){
            if(params.getBaoliCompanyId()!=null&&params.getBaoliCompanyId()!=""){
                entity.setOrgId("1514185640395280384");
                entity.setSite(params.getBaoliCompanyId());
            }else{
                BaoliCompany baoliCompany = new BaoliCompany();
                baoliCompany.setContactPhone(params.getPhone());
                baoliCompany.setContactPerson(params.getUserName());
                baoliCompany.setCompanyName(params.getCompanyName());

                baoliCompany.setId(idWorker.nextId() + "")
                        .preInsert("1");
                this.baoliCompanyMapper.insert(baoliCompany);

                entity.setOrgId("1512956779477209088");
                entity.setSite(params.getCustomerId());
                entity.setBaoliCompanyId(baoliCompany.getId());
            }
        }else{
            if(params.getCustomerId()!=null&&params.getCustomerId()!=""){
                entity.setOrgId("1512956779477209088");
                entity.setSite(params.getCustomerId());
            }else{
                Customer customer = new Customer();
                customer.setContactPhone(params.getPhone());
                customer.setContactName(params.getUserName());
                customer.setCompanyName(params.getCompanyName());
                customer.setCustomerName(params.getCompanyName());
                customer.setCustomerType("企业");
                customer.setProveStatus("NEW");
                customer.setId(idWorker.nextId() + "")
                        .preInsert("1");
                this.mapper.insert(customer);
                entity.setOrgId("1512956779477209088");
                entity.setSite(params.getCustomerId());
                entity.setCustomerId(customer.getId());
            }
        }
        entity.setId(idWorker.nextId() + "")
                .preInsert("1");
        this.userMapper.insert(entity);
        // 新增用户角色
        if (ListUtils.isNotEmpty(params.getRoleIds())) {
            for (String roleId : params.getRoleIds()) {
                UserRoleRelSaveParam userRoleRelSaveParam = new UserRoleRelSaveParam();
                userRoleRelSaveParam.setUserId(entity.getId())
                        .setRoleId(roleId);
                userRoleRelService.insert(userRoleRelSaveParam);
            }
        }else{
            if(params.getUserType().equals("企业")){
                UserRoleRelSaveParam userRoleRelSaveParam = new UserRoleRelSaveParam();
                userRoleRelSaveParam.setUserId(entity.getId())
                        .setRoleId("1512963384033808384");
                UserRoleRel userRoleRel = new UserRoleRel();
                BeanUtils.copyProperties(userRoleRelSaveParam, userRoleRel);
                userRoleRel.setId(idWorker.nextId() + "").preInsert("1");
                this.userRoleRelMapper.insert(userRoleRel);
            }else if(params.getUserType().equals("银行")){
                UserRoleRelSaveParam userRoleRelSaveParam = new UserRoleRelSaveParam();
                userRoleRelSaveParam.setUserId(entity.getId())
                        .setRoleId("1512962180969009152");
                UserRoleRel userRoleRel = new UserRoleRel();
                BeanUtils.copyProperties(userRoleRelSaveParam, userRoleRel);
                userRoleRel.setId(idWorker.nextId() + "").preInsert("1");
                this.userRoleRelMapper.insert(userRoleRel);

            }
        }
        // 新增用户岗位

        return entity.getId();
    }
    /**
     * 导入
     * @param data
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public TWhWarehouseCustomerMaterial importData(TWhWarehouseCustomerMaterialVo data, UserVo user, int itemNo, int line) {
        data = this.removeSpecialChar(data);
        TWhWarehouseCustomerMaterial tWhWarehouseCustomerMaterial = this.addNewItem(data, user, itemNo, line);
        return tWhWarehouseCustomerMaterial;

    }
    @Transactional(readOnly = false)
    @Override
    public TWhWarehouseCustomerMaterial addNewItem( TWhWarehouseCustomerMaterialVo data,UserVo user,int itemNo,int line) {

        TWhWarehouseCustomerMaterial tWhWarehouseCustomerMaterial = new TWhWarehouseCustomerMaterial();


        String itemErrorMsg = "保存：";
        BeanUtils.copyProperties(data, tWhWarehouseCustomerMaterial);
        TWhWarehouseCustomerMaterial ifExsititems = this.tWhWarehouseCustomerMaterialMapper.findByCustNameAndSkuCode(data.getCustomerName(), data.getSkuCode());

        if (ifExsititems != null) {   BeanUtils.copyProperties(data, ifExsititems);
            ifExsititems.preUpdate(user.getId());
            this.tWhWarehouseCustomerMaterialMapper.updateById(ifExsititems);
            return ifExsititems;
        }else{
            Customer customer = this.mapper.findByName(data.getCustomerName());
            if(customer!=null){
                tWhWarehouseCustomerMaterial.setCustomerNo(customer.getId());
            }else{
                customer=this.genNewCust(data.getCustomerName(),"supplier",user);
                tWhWarehouseCustomerMaterial.setCustomerNo(customer.getId());
                this.genNewSupplier(data.getCustomerName(),user);
            }
            Sku sku = this.skuMapper.findByNameAndSkuCode(data.getSkuName(),data.getSkuCode());
            if(sku!=null){
                tWhWarehouseCustomerMaterial.setMaterialNo(sku.getId());
            }else{

//                Spu spu =this.spuMapper.findByName(data.getSkuName());
                Spu spu =this.spuMapper.findByCode(data.getSkuCode());
                if(spu==null){
                    spu = this.genNewSpu(data.getSkuCode(),data.getSkuName(),user);
                }
                sku = this.genNewSku(data.getSkuName(),data.getSkuCode(),spu,user);
                tWhWarehouseCustomerMaterial.setMaterialNo(sku.getId());
            }
            TbWmsContainBoxType tbWmsContainBoxType = this.tbWmsContainBoxTypeMapper.
                    findByNameAndSpec(data.getInBoundSizeDetail(),data.getInBoundPackType());
            if(tbWmsContainBoxType==null){
                this.genContainerType(data.getInBoundSizeDetail(),data.getInBoundPackType(),user);
            }
            TbWmsContainBoxType delivertbWmsContainBoxType = this.tbWmsContainBoxTypeMapper.
                    findByNameAndSpec(data.getInBoundSizeDetail(),data.getInBoundPackType());
            if(data.getInBoundSizeDetail().contains("*")){
                String sized= data.getInBoundSizeDetail().replace("*","X") ;
                data.setInBoundSizeDetail(sized);
            }
            if(delivertbWmsContainBoxType==null){
                this.genContainerType(data.getInBoundSizeDetail(),data.getDeliveryPackType(),user);
            }

            String sizes[] = data.getInBoundSizeDetail().split("X");
            Double vol = NumberUtils.mul(Double.valueOf(sizes[0]),Double.valueOf(sizes[1]));
            Double volume = NumberUtils.mul(Double.valueOf(sizes[2]),vol);
            tWhWarehouseCustomerMaterial.setUnitVolume(BigDecimal.valueOf(volume));
            tWhWarehouseCustomerMaterial.setPackLong(Double.valueOf(sizes[0]));
            tWhWarehouseCustomerMaterial.setPackWidth(Double.valueOf(sizes[1]));
            tWhWarehouseCustomerMaterial.setPackHeight(Double.valueOf(sizes[2]));
            tWhWarehouseCustomerMaterial.setSpec(sku.getSpec());
            if(tWhWarehouseCustomerMaterial.getShipByPackOrqty()!=null){
                if(tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("件")||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("箱")
                        ||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("卷")){
                    tWhWarehouseCustomerMaterial.setShipByPackOrqty("pack");
                }else if(tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("个")){
                    tWhWarehouseCustomerMaterial.setShipByPackOrqty("qty");
                }else if(tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("吨")||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("公斤")
                        ||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("千克")||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("克")
                        ||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("kg")){
                    tWhWarehouseCustomerMaterial.setShipByPackOrqty("weight");
                }else if(tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("立方")||tWhWarehouseCustomerMaterial.getShipByPackOrqty().equals("m3")){
                    tWhWarehouseCustomerMaterial.setShipByPackOrqty("vol");
                }else{
                    throw new BusinessException("结算基于没有办法转换，请联系It");
                }
            }else{
                if(tWhWarehouseCustomerMaterial.getMeasUnitNo()!=null){
                    if(tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("件")||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("箱")
                            ||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("卷")){
                        tWhWarehouseCustomerMaterial.setShipByPackOrqty("pack");
                    }else if(tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("个")){
                        tWhWarehouseCustomerMaterial.setShipByPackOrqty("qty");
                    }else if(tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("吨")||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("公斤")
                            ||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("千克")||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("克")
                            ||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("kg")){
                        tWhWarehouseCustomerMaterial.setShipByPackOrqty("weight");
                    }else if(tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("立方")||tWhWarehouseCustomerMaterial.getMeasUnitNo().equals("m3")){
                        tWhWarehouseCustomerMaterial.setShipByPackOrqty("vol");
                    }else{
                        throw new BusinessException("结算基于没有办法转换，请联系It");
                    }
                }
            }
            tWhWarehouseCustomerMaterial.setId(idWorker.nextId() + "")
                    .preInsert(user.getId());
            this.tWhWarehouseCustomerMaterialMapper.insert(tWhWarehouseCustomerMaterial);
            return tWhWarehouseCustomerMaterial;
        }
    }
    @Override
    public void genContainerType(String detail,String deliveryPackType,UserVo user){
        String sizes[] = null;
        if(detail.contains("-")){
             sizes = detail.split("-");
        }
        if(detail.contains("X")){
            sizes = detail.split("X");
        }
        if(detail.contains("/")){
            sizes = detail.split("/");
        }
        if(detail.contains(";")){
            sizes = detail.split(";");
        }
        if(detail.contains("*")){
            sizes = detail.split("\\*");
        }
        TbWmsContainBoxType tbWmsContainBoxType = new TbWmsContainBoxType();
        Double vol = NumberUtils.mul(Double.valueOf(sizes[0]),Double.valueOf(sizes[1]));
        Double volume = NumberUtils.mul(Double.valueOf(sizes[2]),vol);
        tbWmsContainBoxType.setMaxVol(volume.longValue());
        tbWmsContainBoxType.setLength(Long.valueOf(sizes[0]));
        tbWmsContainBoxType.setWidth(Long.valueOf(sizes[1]));
        tbWmsContainBoxType.setHeight(Long.valueOf(sizes[2]));
        tbWmsContainBoxType.setSpec(detail);
        tbWmsContainBoxType.setName(deliveryPackType);
        tbWmsContainBoxType.setType(deliveryPackType);
        tbWmsContainBoxType.setId(idWorker.nextId() + "")
                .preInsert(user.getId());
        this.tbWmsContainBoxTypeMapper.insert(tbWmsContainBoxType);

    }
    @Override
    public Spu genNewSpu(String skuCode, String skuName,UserVo user){
        Spu spu = new Spu();
        spu.setSite(user.getOrgId());
        spu.setSpuCode(skuCode);
        spu.setSpuName(skuName);
        spu.setId(idWorker.nextId() + "")
                .preInsert(user.getId());
        this.spuMapper.insert(spu);
        return spu;
    }
    @Override
    public Sku genNewSku(String skuName,String skuCode, Spu spu,UserVo user){
        Sku sku = new Sku();
        sku.setSite(user.getOrgId());
        sku.setSpuId(spu.getId());
        sku.setSkuCode(skuCode);
        sku.setCustSkuId(skuCode);
        sku.setId(idWorker.nextId() + "")
                .preInsert(user.getId());
        this.skuMapper.insert(sku);
        return sku;
    }
    @Override
    public Customer genNewCust(String customerName, String type,UserVo user){
        Customer customer = new Customer();
        customer.setCustomerType(type);
        customer.setCustomerName(customerName);
        customer.setId(idWorker.nextId() + "")
                .preInsert(user.getId());
        this.mapper.insert(customer);
        return customer;
    }
    @Override
    public Supplier genNewSupplier(String customerName,UserVo user){
        Supplier supplier = new Supplier();
        supplier.setSupplierName(customerName);
        supplier.setSupplierFullName(customerName);
        supplier.setCustomerNo(user.getCustomerId());
        supplier.setCustomerName(user.getCompanyName());
        supplier.setId(idWorker.nextId() + "")
                .preInsert(user.getId());
        this.supplierMapper.insert(supplier);
        return supplier;
    }

    @Override
    public TWhWarehouseCustomerMaterialVo removeSpecialChar(TWhWarehouseCustomerMaterialVo input){
        Pattern p =null;
        Matcher m = null;;
        String rt = "";
        if(!StringUtils.isNull(input.getCustomerName())||input.getCustomerName()==""){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCustomerName());
            rt = m.replaceAll("");
            input.setCustomerName(rt);
        }
        if(!StringUtils.isNull(input.getSkuCode())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSkuCode());
            rt = m.replaceAll("");
            input.setSkuCode(rt);
        }
        if(!StringUtils.isNull(input.getSkuName())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getSkuName());
            rt = m.replaceAll("");
            input.setSkuName(rt);
        }

        if(!StringUtils.isNull(input.getConfigRate())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getConfigRate());
            rt = m.replaceAll("");
            input.setConfigRate(rt);
        }

        if(!StringUtils.isNull(input.getMonthlySupplyQty())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getMonthlySupplyQty());
            rt = m.replaceAll("");
            input.setMonthlySupplyQty(rt);
        }

        if(!StringUtils.isNull(input.getAcceptWarehouse())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getAcceptWarehouse());
            rt = m.replaceAll("");
            input.setAcceptWarehouse(rt);
        }

        if(!StringUtils.isNull(input.getQtyusedPerVehicle())) {
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getQtyusedPerVehicle());
            rt = m.replaceAll("");
            input.setQtyusedPerVehicle(rt);
        }
        if(!StringUtils.isNull(input.getUnitWeight())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getUnitWeight());
            rt = m.replaceAll("");
            input.setUnitWeight(rt);
        }
        if(!StringUtils.isNull(input.getShipmentType())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getShipmentType());
            rt = m.replaceAll("");
            input.setShipmentType(rt);
        }
        if(!StringUtils.isNull(input.getInBoundPackType())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getInBoundPackType());
            rt = m.replaceAll("");
            input.setInBoundPackType(rt);
        }
        if(!StringUtils.isNull(input.getInBoundSizeDetail())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getInBoundSizeDetail());
            rt = m.replaceAll("");

            input.setInBoundSizeDetail(rt);

        }
        if(!StringUtils.isNull(input.getQtyPerPack())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getQtyPerPack());
            rt = m.replaceAll("");
            input.setQtyPerPack(rt);

        }
        if(!StringUtils.isNull(input.getCanPileUpYn())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCanPileUpYn());
            rt = m.replaceAll("");

            input.setCanPileUpYn(rt);

        }
        if(!StringUtils.isNull(input.getCanPileLays())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getCanPileLays());
            rt = m.replaceAll("");
            input.setCanPileLays(rt);
        }
        if(!StringUtils.isNull(input.getPackPerPallet())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getPackPerPallet());
            rt = m.replaceAll("");
            input.setPackPerPallet(rt);
        }
        if(!StringUtils.isNull(input.getTransitWarehouseMaxInventory())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getTransitWarehouseMaxInventory());
            rt = m.replaceAll("");
            input.setTransitWarehouseMaxInventory(rt);
        }
        if(!StringUtils.isNull(input.getDeliveryPackType())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getDeliveryPackType());
            rt = m.replaceAll("");
            input.setDeliveryPackType(rt);
        }
        if(!StringUtils.isNull(input.getDeliveryPackSize())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getDeliveryPackSize());
            rt = m.replaceAll("");
            input.setDeliveryPackSize(rt);
        }
        if(!StringUtils.isNull(input.getDeliveryQtyPerPack())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getDeliveryQtyPerPack());
            rt = m.replaceAll("");
            input.setDeliveryQtyPerPack(rt);
        }
        if(!StringUtils.isNull(input.getWarehouseExpectedUsed())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getWarehouseExpectedUsed());
            rt = m.replaceAll("");
            input.setWarehouseExpectedUsed(rt);
        }
        if(!StringUtils.isNull(input.getAttentionRemark())){
            p = Pattern.compile("\\s*|\t|\r|\n");
            m = p.matcher(input.getAttentionRemark());
            rt = m.replaceAll("");
            input.setAttentionRemark(rt);
        }


        return input;
    }
}
